package com.luobr.backend.api.imageInfo;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: 罗念笙
 * @date: 2025/3/22 15:29
 * @description: 图片信息获取（比如：主色调、缩略图等等）
 */
@Slf4j
public class ImageInfoGetApi {

    /**
     * 获取图片主色调
     *
     * @param imageUrl 图片完整的 url
     * @return 比如：0xbbcd7f
     */
    public static String getImageColor(String imageUrl) {
        Map<String, String> result = new HashMap<>();
                // 请求 url 格式： imageUrl + ?x-oss-process=image/average-hue
        String url = imageUrl + "?x-oss-process=image/average-hue";
        try {
            // 发起GET请求
            HttpResponse response = HttpUtil.createGet(url).execute();
            String responseBody = response.body();
            result = JSONUtil.toBean(responseBody, Map.class);
        } catch (Exception e) {
            log.error("获取主色调失败，图片路径：" + imageUrl, e);
        }
        return result.getOrDefault("RGB", "");
    }

    /**
     * 测试接口
     * @param args
     */
    public static void main(String[] args) {
        String url = "你的图片url" + "?x-oss-process=image/average-hue";
        // 发起GET请求
        HttpResponse response = HttpUtil.createGet(url).execute();
        String responseBody = response.body();
        Map<String, String> result = JSONUtil.toBean(responseBody, Map.class);
        System.out.println();
    }
}